⚡️ Speed up function should_propagate_trace by 245%#45
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up function should_propagate_trace by 245%#45codeflash-ai[bot] wants to merge 1 commit intomasterfrom
should_propagate_trace by 245%#45codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimization introduces **regex compilation caching** to `match_regex_list`, which provides dramatic performance improvements when the same regex patterns are matched repeatedly. **Key Changes:** - **Regex Compilation Caching**: Instead of calling `re.search()` with raw strings (which internally compiles patterns each time), the code now pre-compiles all patterns using `re.compile()` and caches them based on the `regex_list` identity and `substring_matching` flag. - **Pattern Preparation**: The logic for appending `$` anchors is moved to the caching phase, avoiding repeated string operations during matching. **Why This Speeds Up:** - **Eliminates Redundant Compilation**: The original code recompiled the same regex patterns on every call. With caching, patterns are compiled once and reused across multiple invocations. - **Reduces String Operations**: Pattern modification (adding `$`) happens only during cache creation, not on every match attempt. **Performance Benefits by Test Case:** - **Massive gains for repeated pattern usage**: Tests with large regex lists show 60,000-770,000% speedups (e.g., `test_large_many_trace_targets_one_match`: 27.3ms → 3.77μs) - **Excellent for complex patterns**: Regex-heavy tests like `test_edge_targets_with_special_regex` show 2,295% speedup - **Minimal overhead for simple cases**: Basic single-regex tests show slight slowdown (7-8%) due to caching overhead, but this is negligible compared to the gains in realistic usage scenarios The optimization is particularly effective for Sentry's trace propagation use case, where the same `trace_propagation_targets` list is likely checked against many different URLs throughout an application's lifetime.
misrasaurabh1
approved these changes
Oct 2, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 245% (2.45x) speedup for
should_propagate_traceinsentry_sdk/tracing_utils.py⏱️ Runtime :
202 milliseconds→58.5 milliseconds(best of28runs)📝 Explanation and details
The optimization introduces regex compilation caching to
match_regex_list, which provides dramatic performance improvements when the same regex patterns are matched repeatedly.Key Changes:
re.search()with raw strings (which internally compiles patterns each time), the code now pre-compiles all patterns usingre.compile()and caches them based on theregex_listidentity andsubstring_matchingflag.anchors is moved to the caching phase, avoiding repeated string operations during matching.
Why This Speeds Up:
) happens only during cache creation, not on every match attempt.
Performance Benefits by Test Case:
test_large_many_trace_targets_one_match: 27.3ms → 3.77μs)test_edge_targets_with_special_regexshow 2,295% speedupThe optimization is particularly effective for Sentry's trace propagation use case, where the same
trace_propagation_targetslist is likely checked against many different URLs throughout an application's lifetime.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
tracing/test_misc.py::test_should_propagate_tracetracing/test_misc.py::test_should_propagate_trace_to_sentry🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-should_propagate_trace-mg9mvvshand push.